﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Web.Mail;
using System.Net.Mail;

public partial class RequestForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.txtAmount.Visible = false;
        //post back
        if (!Page.IsPostBack)
        {
            this.calDate.Visible = true;
            this.calDate.Style.Add("display", "none");
            if (Session["user"] != null)
            {
                this.Log_in1.Visible = false;
                this.Log_out1.Visible = true;
            }
            else
            {
                Response.Redirect("Login.aspx");

            }
            LoadCbbInfo();
            if (Request.Params.Get("rqid") != null)
            {
                //update
                ProRequest pr = new ProRequest();
                DataSet dsRquestInfo = new DataSet();
                dsRquestInfo = pr.LoadRequestInfo(Request.Params.Get("rqid"));
                //if (dsRquestInfo.Tables[0].Rows.Count == 0)
                //{
                //    this.lblError.Text = "You don't have this Request form!";
                //    this.Request.Params.Clear();
                //    return;
                //}
                if (Session["user"].ToString() != dsRquestInfo.Tables[0].Rows[0]["username"].ToString())
                {
                    this.lblError.Text = "You don't have permission to edit this request or you aren't submitter. Rediect to Review page afer 5s";
                    HtmlMeta metaTag = new HtmlMeta();
                    metaTag.Attributes.Add("http-equiv", "refresh");
                    metaTag.Attributes.Add("content", "5;url=RequestReview.aspx?rqid=" + Request.Params.Get("rqid"));
                    HtmlHead head = (HtmlHead)Page.Header;
                    head.Controls.Add(metaTag);
                }
                else
                    SetIndexForCbb(dsRquestInfo);

            }
            else
            {
                //create new request form
                //Set Calendar
                this.calDate.SelectedDate = DateTime.Now.Date;
                this.lblDate.Text = DateTime.Now.Date.ToShortDateString();
                this.txtAmount.Text = "0";
            }

        }
        else
        {
        }
    }

    private void SetIndexForCbb(DataSet dsRquestInfo)
    {
        this.cbbBusCat.SelectedValue = dsRquestInfo.Tables[0].Rows[0]["division"].ToString();
        this.cbbBusInfo.SelectedValue = dsRquestInfo.Tables[0].Rows[0]["bp_user"].ToString();
        this.cbbDirector.SelectedValue = dsRquestInfo.Tables[0].Rows[0]["director"].ToString();
        this.cbbPIC.SelectedValue = dsRquestInfo.Tables[0].Rows[0]["pic"].ToString();
        this.cbbVPre.SelectedValue = dsRquestInfo.Tables[0].Rows[0]["vpresident"].ToString();
        this.lblDate.Text = DateTime.Parse(dsRquestInfo.Tables[0].Rows[0]["date"].ToString()).ToShortDateString();
        this.calDate.SelectedDate = DateTime.Parse(dsRquestInfo.Tables[0].Rows[0]["date"].ToString());
        this.txtDescription.Text = dsRquestInfo.Tables[0].Rows[0]["description"].ToString();
        this.txtRemark.Text = dsRquestInfo.Tables[0].Rows[0]["remark"].ToString();
        this.txtAmount.Text = dsRquestInfo.Tables[0].Rows[0]["amount"].ToString();
    }

    protected void LoadCbbInfo()
    {
        //********** Hoang.Nguyen 2010-02-04 **********//
        //Load Combobox Division: Business Catalogs
        ProRequest pr = new ProRequest();
        DataSet ds = new DataSet();
        ds = pr.LoadBusCat();
        this.cbbBusCat.DataSource = ds.Tables[0];
        this.cbbBusCat.DataValueField = "id";
        this.cbbBusCat.DataTextField = "catalog_name";
        this.cbbBusCat.DataBind();

        //Load Combobox PIC, Director, V.Present
        ds = new DataSet();
        ds = pr.LoadPic();
        this.cbbPIC.DataSource = ds.Tables[0];
        this.cbbPIC.DataValueField = "username";
        this.cbbPIC.DataTextField = "fullname";
        this.cbbPIC.DataBind();

        ds = new DataSet();
        ds = pr.LoadVPre();
        this.cbbVPre.DataSource = ds.Tables[0];
        this.cbbVPre.DataValueField = "username";
        this.cbbVPre.DataTextField = "fullname";
        this.cbbVPre.DataBind();

        ds = new DataSet();
        ds = pr.LoadDirector();
        this.cbbDirector.DataSource = ds.Tables[0];
        this.cbbDirector.DataValueField = "username";
        this.cbbDirector.DataTextField = "fullname";
        this.cbbDirector.DataBind();

        //Load combobox BusinessInfo
        ds = new DataSet();
        ds = pr.LoadListPartner();
        this.cbbBusInfo.DataSource = ds.Tables[0];
        this.cbbBusInfo.DataValueField = "code";
        this.cbbBusInfo.DataTextField = "company";
        this.cbbBusInfo.DataBind();
        //********** Hoang.Nguyen 2010-02-04 **********//
    }

    public void sentmail()
    {

        // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
        // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        try
        {
            MailAddress fromAddress = new MailAddress("nguyenvanhoanglk@hotmail.com", "abc");

            // You can specify the host name or ipaddress of your server
            // Default in IIS will be localhost 
            smtpClient.Host = "localhost";

            //Default port will be 25
            smtpClient.Port = 25;

            //From address will be given as a MailAddress Object
            message.From = fromAddress;

            // To address collection of MailAddress
            message.To.Add("nguyenvanhoanglk@hotmail.com");
            message.Subject = "Forgot Password for username " + " at Techdata";

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("admin1@yoursite.com")
            //message.CC.Add("nguyenvanhoanglk@hotmail.com");
            //message.CC.Add("nguyenvanhoanglk@hotmail.com");

            // You can specify Address directly as string
            //message.Bcc.Add(new MailAddress("nguyenvanhoanglk@hotmail.com"));
            //message.Bcc.Add(new MailAddress("nguyenvanhoanglk@hotmail.com"));

            //Body can be Html or text format
            //Specify true if it  is html message
            message.IsBodyHtml = false;

            // Message body content
            message.Body = "hế lô";

            // Send SMTP mail
            smtpClient.Send(message);

            //lblStatus.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            //lblStatus.Text = "Send Email Failed." + ex.Message;
        }
    }
    protected void imgCalendar_Click(object sender, ImageClickEventArgs e)
    {
        //if (this.calDate.Visible == false)
        //    this.calDate.Visible = true;
        //else
        //    this.calDate.Visible = false;
    }

    protected void cmdClear_Click(object sender, EventArgs e)
    {
        this.lblDate.Text = DateTime.Now.ToShortDateString();
        this.txtAmount.Text = "0";
        this.txtDescription.Text = Constant.STR_DEFAULT;
        this.txtRemark.Text = Constant.STR_DEFAULT;
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        if (Session["user"].ToString() == null)
            Response.Redirect("login.aspx");

        ProRequest pr = new ProRequest();
        RequestInfo rq = new RequestInfo();

        rq.UiSubmitter.StrUserName = Session["user"].ToString();
        rq.StrDescription = this.txtRemark.Text;
        rq.StrRemark = this.txtRemark.Text;

        rq.DtRequestDate = DateTime.Parse(this.lblDate.Text);
        rq.FAmount = 0;// Int32.Parse(this.txtAmount.Text);

        rq.StRequestStatus.Id = Constant.REQUEST_STATUS_PENDING;
        rq.StRequestStatus.StrStatus = Constant.STR_STATUS_DEFAULT_REQUEST;

        rq.BcBusCatDivision.Id = Int32.Parse(this.cbbBusCat.SelectedItem.Value);
        rq.BcBusCatDivision.StrCatalogeName = this.cbbBusCat.SelectedItem.Text;

        rq.BiBusPartner.StrBusPartCode = this.cbbBusInfo.SelectedItem.Value;
        rq.BiBusPartner.StrCompany = this.cbbBusInfo.SelectedItem.Text;

        rq.UiDirector.StrUserName = this.cbbDirector.SelectedItem.Value;
        rq.UiDirector.StrFullName = this.cbbDirector.SelectedItem.Text;

        rq.UiPIC.StrUserName = this.cbbPIC.SelectedItem.Value;
        rq.UiPIC.StrFullName = this.cbbPIC.SelectedItem.Text;

        rq.UiVPre.StrUserName = this.cbbVPre.SelectedItem.Value;
        rq.UiVPre.StrFullName = this.cbbVPre.SelectedItem.Text;

        if (Request.Params.Get("rqid") != null)
        {
            //update or reject
            rq.StrRequestCode = Request.Params.Get("rqid");
            DataSet ds = pr.LoadRequestConfirmInfo(rq.StrRequestCode);
            if (ds == null || ds.Tables[0].Rows.Count == 0 || (ds != null && (ds.Tables[0].Rows[0]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString() ||
                                                ds.Tables[0].Rows[1]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString() ||
                                                ds.Tables[0].Rows[2]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString())))
            {
                if (pr.UpdateRequestInfo(rq))
                    Response.Redirect("RequestDetail.aspx?rqid=" + rq.StrRequestCode);
                else
                    lblError.Text = "Can't update request form, please review your information";
            }
            else
            {//is processing
                lblError.Text = "This request is being processing! You can't modify. After 10s the page will be redirected to detail page automatically";

                HtmlMeta metaTag = new HtmlMeta();
                metaTag.Attributes.Add("http-equiv", "refresh");
                metaTag.Attributes.Add("content", "10;url=RequestReview.aspx?rqid=" + rq.StrRequestCode);
                HtmlHead head = (HtmlHead)Page.Header;
                head.Controls.Add(metaTag);
            }
        }
        else
        {
            rq.StrRequestCode = pr.GenerateRequestCode();
            if (rq.StrRequestCode == "")
                return;
            //insert new request
            if (pr.AddRequestInfo(rq))
                Response.Redirect("RequestDetail.aspx?rqid=" + rq.StrRequestCode);
            else
                lblError.Text = "Can't create new request form, please check your information!";
        }
    }
    protected void calDate_SelectionChanged(object sender, EventArgs e)
    {
        this.lblDate.Text = this.calDate.SelectedDate.ToShortDateString();
    }
}
